package com.becca.cake.controller.coupon;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.becca.cake.core.common.BaseAction;
import com.becca.cake.core.common.PagingBean;
import com.becca.cake.core.exception.CommonExceptionHandle;
import com.becca.cake.model.coupon.CouponModel;
import com.becca.cake.model.coupon.CouponSaleSlipModel;
import com.becca.cake.service.coupon.ICouponService;
import com.becca.cake.util.JsonUtil;

/**
 * 优惠管理
 * @copyright  尊星(上海)信息科技有限公司
 * @author hejinguo
 * @date 2016年7月1日 上午9:02:38
 */
@Controller
@RequestMapping("/couponManager")
public class CouponAction extends BaseAction {
    private static final Logger logger = LoggerFactory.getLogger(CouponAction.class);
    @Autowired
    private ICouponService      iCouponService;

    /**
     * 优惠码查询列表
     * @param ruleGroupModel
     * @param existall
     * @return
     */
    @RequestMapping(value = "/selectCoupon", method = RequestMethod.POST)
    @ResponseBody
    public String selectCoupon(CouponModel couponModel) {
        Map<String, Object> map = new HashMap<String, Object>();
        PagingBean page = getInitPagingBean();
        map.put("pagingBean", page);
        map.put("couponModel", couponModel);
        try {
            // step1:优惠码查询
            List<CouponModel> list = this.iCouponService.selectCoupon(map);
            // step2:获取优惠码总条数
            Long count = this.iCouponService.selectCouponCount(map);
            return JsonUtil.writeObjectPageJSON(list, count);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 根据id查询优惠码对象
     * @param id
     * @return
     */
    @RequestMapping(value = "/queryCouponById", method = RequestMethod.POST)
    @ResponseBody
    public String queryCouponById(@RequestParam(value = "id", required = false) Integer id) {
        try {
            CouponModel couponModel = this.iCouponService.queryCouponById(id);
            return JsonUtil.writeObjectFormJSON(couponModel);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 添加或修改优惠码
     * @param ruleGroupModel
     * @return
     */
    @RequestMapping(value = "/addOrUpdCoupon", method = RequestMethod.POST)
    @ResponseBody
    public String addOrUpdCoupon(CouponModel couponModel,
                                 @RequestParam(value = "isAutomatic", required = false) String isAutomatic,
                                 @RequestParam(value = "couponCodeNumber", required = false) Integer couponCodeNumber) {
        try {
            this.iCouponService.insertOrUpdateCoupon(couponModel, isAutomatic, couponCodeNumber,
                this.getUserSeesion());
            return JsonUtil.returnJsonSuccess("保存优惠码信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 优惠码导出excel
     * @param request
     * @param response
     * @param jsonList
     */
    @RequestMapping(value = "/exportCouponsToExcel", method = RequestMethod.GET)
    public void exportCouponsToExcel(HttpServletRequest request,
                                     HttpServletResponse response,
                                     @RequestParam(value = "jsonList", required = false) String jsonList) {
        try {
            this.iCouponService.exportCouponsToExcel(request, response, jsonList);
        } catch (Exception e) {
            logger.error("优惠码导出excel失败!");
        }
    }

    /**
     * 激活优惠码
     * @param ruleGroupModel
     * @return
     */
    @RequestMapping(value = "/updateActivatedCoupon", method = RequestMethod.POST)
    @ResponseBody
    public String updateActivatedCoupon(@RequestParam(value = "couponIds", required = false) String couponIds,
                                        @RequestParam(value = "couponType", required = false) Byte couponType,
                                        @RequestParam(value = "couponEndDate", required = false) String couponEndDate,
                                        @RequestParam(value = "salesName", required = false) String salesName) {
        try {
            this.iCouponService.updateActivatedCoupon(couponIds, couponType, couponEndDate,salesName,
                this.getUserSeesion());
            return JsonUtil.returnJsonSuccess("激活优惠码成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 销售信息查询
     * @param couponSaleSlipModel
     * @return
     */
    @RequestMapping(value = "/selectCouponSale", method = RequestMethod.POST)
    @ResponseBody
    public String selectCouponSale(CouponSaleSlipModel couponSaleSlipModel) {
        PagingBean page = getInitPagingBean();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("couponSaleSlipModel", couponSaleSlipModel);
        map.put("pagingBean", page);
        try {
            List<CouponSaleSlipModel> list = this.iCouponService.selectCouponSale(map);
            Long count = this.iCouponService.selectCouponSaleSlipCount(map);
            return JsonUtil.writeObjectPageJSON(list, count);
        } catch (Exception e) {
            logger.error("查询销售信息失败!", e);
            return JsonUtil.returnJsonUnSuccess("查询销售信息失败!");
        }

    }

    /**
     * 根据id查询销售信息
     * @param couponSaleId
     * @return
     */
    @RequestMapping(value = "/selectCouponSaleById", method = RequestMethod.POST)
    @ResponseBody
    public String selectCouponSaleById(@RequestParam("couponSaleId") Integer couponSaleId) {
        try {
            //step1:请求参数
            CouponSaleSlipModel couponSaleSlipModel = this.iCouponService
                .selectCouponSaleById(couponSaleId);
            return JsonUtil.writeObjectFormJSON(couponSaleSlipModel);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 添加或修改销售信息
     * @author yellow
     * @return
     */
    @RequestMapping(value = "/insertOrUpdateCouponSale", method = RequestMethod.POST)
    @ResponseBody
    public String insertOrUpdateCouponSale(CouponSaleSlipModel couponSaleSlipModel) {
        try {
            this.iCouponService
                .insertOrUpdateCouponSale(couponSaleSlipModel, this.getUserSeesion());
            return JsonUtil.returnJsonSuccess("操作销售信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

    /**
     * 删除销售信息
     * @author mouwanming
     * @return
     */
    @RequestMapping(value = "/deleteCouponSaleById", method = RequestMethod.POST)
    @ResponseBody
    public String deleteCouponSaleById(@RequestParam("couponSaleId") Integer couponSaleId) {
        try {
            this.iCouponService.deleteCouponSaleById(couponSaleId);
            return JsonUtil.returnJsonSuccess("删除销售信息成功!");
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }
    
    /**
     * 优惠码查询列表
     * @param ruleGroupModel
     * @param existall
     * @return
     */
    @RequestMapping(value = "/selectCouponStatus", method = RequestMethod.POST)
    @ResponseBody
    public String selectCouponStatus(@RequestParam(value = "couponCode", required = false) String couponCode) {
        Map<String, Object> map = new HashMap<String, Object>();
        PagingBean page = getInitPagingBean();
        map.put("pagingBean", page);
        map.put("couponCode", couponCode);
        try {
            // step1:优惠码查询
            List<CouponModel> list = this.iCouponService.selectCouponStatus(map);
            // step2:获取优惠码总条数
            Long count = this.iCouponService.selectCouponStatusCount(map);
            return JsonUtil.writeObjectPageJSON(list, count);
        } catch (Exception e) {
            return JsonUtil.returnJsonUnSuccess(CommonExceptionHandle.handleException(e));
        }
    }

}
